package com.trustbook.visitor.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.VideoView;

import com.trustbook.visitor.log.LogManager;
import com.trustbook.visitor.util.CameraUtil;

public class SpyVideoRecorderActivity extends ActionBarActivity {

	private static final String TAG = "SpyVideoRecorderActivity";
	private static String message;
	private static final int ACTION_TAKE_VIDEO = 2000;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_spy_video_recorder);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction().add(R.id.container, new VideoRecoderFragment()).commit();
		}

		// This should be sufficient for below and above versions
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		Intent intent = getIntent();
		message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
		LogManager.debug(TAG, "message :" + message);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.spy_video_recorder, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * Created[onAttach()->onCreate()->onCreateView()->onActivityCreated()]->
	 * started[onStart()]..
	 * 
	 * A placeholder fragment containing a simple view.
	 */
	public static class VideoRecoderFragment extends Fragment {

		VideoView mVideoView;

		public VideoRecoderFragment() {
		}

		@Override
		public void onCreate(Bundle savedInstanceState) {
			LogManager.debug(TAG, "PlaceholderFragment - onCreate() Activity:" + this.getClass());
			super.onCreate(savedInstanceState);

		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			LogManager.debug(TAG, "PlaceholderFragment - onCreateView()  message : " + message);
			View rootView = inflater.inflate(R.layout.fragment_video_recorder, container, false);
			((TextView) rootView.findViewById(R.id.text_message)).setText(message);
			Button button = (Button) rootView.findViewById(R.id.btn_start);
			mVideoView = (VideoView) rootView.findViewById(R.id.videoView1);
			button.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					dispatchTakeVideoIntent();
				}
			});
			return rootView;
		}

		private void dispatchTakeVideoIntent() {
			LogManager.debug(TAG, "dispatchTakeVideoIntent() called.");
			if (CameraUtil.isIntentAvailable(getActivity(), MediaStore.ACTION_VIDEO_CAPTURE)) {
				Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
				startActivityForResult(takeVideoIntent, ACTION_TAKE_VIDEO);
			}
		}

		@Override
		public void onActivityResult(int requestCode, int resultCode, Intent intent) {
			LogManager.debug(TAG, "onActivityResult() called. requestCode :" + requestCode + " resultCode :" + resultCode + " intent :" + intent);
			// get the extras passed via intent parameter.
			Bundle extras = intent.getExtras();
			LogManager.debug(TAG, "--> Bundle extras :" + extras);
			if (resultCode == Activity.RESULT_OK) {
				Uri mVideoUri = intent.getData();
				mVideoView.setVideoURI(mVideoUri);
			}
		}

		@Override
		public void onViewCreated(View view, Bundle savedInstanceState) {
			LogManager.debug(TAG, "PlaceholderFragment - onViewCreated() view :" + view);
			super.onViewCreated(view, savedInstanceState);
		}
	} // end Fragment Activity

}
